package cn.iocoder.yudao.module.wecom.controller.admin.acqlinkuser;

import cn.iocoder.yudao.module.wecom.convert.link.WeLinkUserConverter;
import cn.iocoder.yudao.module.wecom.dal.dataobject.customer.WeCustomerDO;
import cn.iocoder.yudao.module.wecom.dal.dataobject.user.WeUserDO;
import cn.iocoder.yudao.module.wecom.service.customer.WeCustomerService;
import cn.iocoder.yudao.module.wecom.service.user.WeUserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wecom.controller.admin.acqlinkuser.vo.*;
import cn.iocoder.yudao.module.wecom.dal.dataobject.acqlinkuser.AcqLinkUserDO;
import cn.iocoder.yudao.module.wecom.service.acqlinkuser.AcqLinkUserService;

@Tag(name = "管理后台 - 获客链接员工关系")
@RestController
@RequestMapping("/wecom/acq-link-user")
@Validated
public class AcqLinkUserController {

    @Resource
    private AcqLinkUserService acqLinkUserService;

    @Resource
    private WeUserService weUserService;

    @Resource
    private WeCustomerService weCustomerService;

//    @PostMapping("/create")
//    @Operation(summary = "创建获客链接员工关系")
//    @PreAuthorize("@ss.hasPermission('wecom:acq-link-user:create')")
//    public CommonResult<Long> createAcqLinkUser(@Valid @RequestBody AcqLinkUserSaveReqVO createReqVO) {
//        return success(acqLinkUserService.createAcqLinkUser(createReqVO));
//    }
//
//    @PutMapping("/update")
//    @Operation(summary = "更新获客链接员工关系")
//    @PreAuthorize("@ss.hasPermission('wecom:acq-link-user:update')")
//    public CommonResult<Boolean> updateAcqLinkUser(@Valid @RequestBody AcqLinkUserSaveReqVO updateReqVO) {
//        acqLinkUserService.updateAcqLinkUser(updateReqVO);
//        return success(true);
//    }
//
//    @DeleteMapping("/delete")
//    @Operation(summary = "删除获客链接员工关系")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('wecom:acq-link-user:delete')")
//    public CommonResult<Boolean> deleteAcqLinkUser(@RequestParam("id") Long id) {
//        acqLinkUserService.deleteAcqLinkUser(id);
//        return success(true);
//    }

    @GetMapping("/get")
    @Operation(summary = "获得获客链接员工关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wecom:acq-link:query')")
    public CommonResult<AcqLinkUserRespVO> getAcqLinkUser(@RequestParam("id") Long id) {
        AcqLinkUserDO acqLinkUser = acqLinkUserService.getAcqLinkUser(id);
        return success(BeanUtils.toBean(acqLinkUser, AcqLinkUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得获客链接员工关系分页")
    @PreAuthorize("@ss.hasPermission('wecom:acq-link:query')")
    public CommonResult<PageResult<AcqLinkUserRespVO>> getAcqLinkUserPage(@Valid AcqLinkUserPageReqVO pageReqVO) {
        PageResult<AcqLinkUserDO> pageResult = acqLinkUserService.getAcqLinkUserPage(pageReqVO);
        Map<String, WeUserDO> weUserMap = weUserService.getWeUserMap(pageResult.getList().stream().map(AcqLinkUserDO::getWeUserId).collect(Collectors.toList()));
        Map<String, WeCustomerDO> weCustomerMap = weCustomerService.getWeCustomerMap(pageResult.getList().stream().map(AcqLinkUserDO::getWeCustomerId).collect(Collectors.toList()));
        return success(WeLinkUserConverter.INSTANCE.convert(pageResult, weUserMap, weCustomerMap));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出获客链接员工关系 Excel")
    @PreAuthorize("@ss.hasPermission('wecom:acq-link:export')")
    @OperateLog(type = EXPORT)
    public void exportAcqLinkUserExcel(@Valid AcqLinkUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AcqLinkUserDO> list = acqLinkUserService.getAcqLinkUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "获客链接员工关系.xls", "数据", AcqLinkUserRespVO.class,
                        BeanUtils.toBean(list, AcqLinkUserRespVO.class));
    }

}